﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuantitativeInvestment.Model;
using QuantitativeInvestment.Bean;
namespace QuantitativeInvestment.View
{
     partial class ModelForm : Form
    {
        public Model.Model model;
        public MainForm mainForm;
        public bool isNew;
        public ModelForm(Model.Model model,MainForm mainForm,bool isNew)
        {
            this.model = model;
            this.isNew = isNew;
            InitializeComponent();
            generateParameterInputList();
            this.mainForm = mainForm;
        }

        private void generateParameterInputList()
        {
            this.Text = "模型-"+this.model.name; ;
            int labelPosition = 31;
            int textBoxPositon = 28;

            //加载模型需要的参数
            foreach (Parameter p in this.model.paraList.Values)
            {
                
                Label label1 = new System.Windows.Forms.Label();
                // 
                // textBox1
                // 
                if (p.type =="enum")
                {
                    ComboBox comboBox1 = new ComboBox();
                    comboBox1.FormattingEnabled = true;
                    comboBox1.Location = new System.Drawing.Point(100, textBoxPositon);
                    comboBox1.Name = p.name;
                    comboBox1.Size = new System.Drawing.Size(100, 20);
                    comboBox1.TabIndex = 0;
                    foreach (string item in p.enumList)
                    {
                        comboBox1.Items.Add(item);
                    }
                    comboBox1.SelectedText = p.value.ToString();
                    this.Controls.Add(comboBox1);
                }
                else
                {
                    TextBox textBox1 = new System.Windows.Forms.TextBox();
                    textBox1.Location = new System.Drawing.Point(100, textBoxPositon);
                    textBox1.Name = p.name;
                    textBox1.Size = new System.Drawing.Size(100, 21);
                    textBox1.TabIndex = 0;
                    textBox1.Text = p.value.ToString();
                    this.Controls.Add(textBox1);
                }
                // 
                // label1
                // 
                label1.AutoSize = true;
                label1.Location = new System.Drawing.Point(32, labelPosition);
                label1.Name =p.name;
                label1.Size = new System.Drawing.Size(41, 12);
                label1.TabIndex = 1;
                label1.Text =p.name;

                this.Controls.Add(label1);
       
                textBoxPositon = textBoxPositon + 30;
                labelPosition = labelPosition + 30;

            }
            
            //加载模型中因子所需要的参数
            foreach (Factor.Factor factor in this.model.factorList)
            {
                foreach (Parameter p in factor.paraList.Values)
                {
                    Label label1 = new System.Windows.Forms.Label();
                    // 
                    // textBox1
                    // 
                    if (p.type == "enum")
                    {
                        ComboBox comboBox1 = new ComboBox();
                        comboBox1.FormattingEnabled = true;
                        comboBox1.Location = new System.Drawing.Point(100, textBoxPositon);
                        comboBox1.Name = factor.name+"-"+p.name;
                        comboBox1.Size = new System.Drawing.Size(100, 20);
                        comboBox1.TabIndex = 0;
                        foreach (string item in p.enumList)
                        {
                            comboBox1.Items.Add(item);
                        }
                        comboBox1.SelectedText = p.value.ToString();
                        this.Controls.Add(comboBox1);
                    }
                    else
                    {
                        TextBox textBox1 = new System.Windows.Forms.TextBox();
                        textBox1.Location = new System.Drawing.Point(100, textBoxPositon);
                        textBox1.Name = factor.name + "-" + p.name; ;
                        textBox1.Size = new System.Drawing.Size(100, 21);
                        textBox1.TabIndex = 0;
                        textBox1.Text = p.value.ToString();
                        this.Controls.Add(textBox1);
                    }
                    // 
                    // label1
                    // 
                    label1.AutoSize = true;
                    label1.Location = new System.Drawing.Point(32, labelPosition);
                    label1.Name = factor.name + "-" + p.name;
                    label1.Size = new System.Drawing.Size(41, 12);
                    label1.TabIndex = 1;
                    label1.Text = factor.name + "-" + p.name;

                    this.Controls.Add(label1);

                    textBoxPositon = textBoxPositon + 30;
                    labelPosition = labelPosition + 30;

                }
            }


            Button buttonOK = new System.Windows.Forms.Button();
            buttonOK.Location = new System.Drawing.Point(125, textBoxPositon+30);
            buttonOK.Name = "buttonOK";
            buttonOK.Size = new System.Drawing.Size(75, 23);
            buttonOK.TabIndex = 0;
            if (isNew)
                buttonOK.Text = "插入";
            else
                buttonOK.Text = "修改";
            buttonOK.UseVisualStyleBackColor = true;
            buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            this.Controls.Add(buttonOK);
            this.ClientSize = this.ClientSize = new System.Drawing.Size(230, textBoxPositon+80);

            this.Location = new System.Drawing.Point(100, 100); ;

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
           foreach(Control input in this.Controls)
           {
               if (input.GetType().ToString() == "System.Windows.Forms.TextBox" || input.GetType().ToString() == "System.Windows.Forms.ComboBox")
               {
                   string name = input.Name;
                   string  value = input.Text;
                   int factorNamePosition = name.IndexOf("-");
                   if (factorNamePosition>0)
                   {
                       string factorName = name.Substring(0, factorNamePosition);
                       string factorPName = name.Substring(factorNamePosition + 1, name.Length - factorNamePosition-1);
                       foreach (Factor.Factor factor in this.model.factorList)
                       {
                           if (factor.name == factorName)
                           {
                               factor.paraList[factorPName].value = value;
                           }
                       }
                   }
                   else
                     this.model.paraList[name].value = value;
               }
           }
           if (isNew)
           {
               this.mainForm.container.modelList.Add(model);
               model.container = this.mainForm.container;
           }
           this.mainForm.updateModelView();
           this.Dispose();
        }
    }
}
